<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <!-- <el-col :lg="4" :md="12">
            <el-form-item label="商品名称:">
              <el-input clearable v-model="where.name" placeholder="请输入商品名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="上架状态:">
              <el-select clearable v-model="where.status" placeholder="请选择类型" class="ele-fluid">
                <el-option label="全部" value="" />
                <el-option label="上架" value="1" />
                <el-option label="下架" value="2" />
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :lg="8" :md="12">
            <el-form-item label="日期:">
              <el-date-picker
                style="width: 100%"
                v-model="where.time_range"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                label-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 搜索场景 -->
        <template slot="status" slot-scope="{ row }">
          <el-tag v-if="row.status === 1" type="success" size="small"
            >上架</el-tag
          >
          <el-tag v-if="row.status === 2" type="error" size="small"
            >下架</el-tag
          >
        </template>

        <template slot="type" slot-scope="{ row }">
          <el-tag v-if="row.type === 0" type="success" size="small"
            >普通商品</el-tag
          >
          <el-tag v-if="row.type === 1" type="danger" size="small"
            >活动商品</el-tag
          >
        </template>
        <!-- 状态列 -->
        <template slot="get_result" slot-scope="{ row }">
          <el-tag v-if="row.get_result === 1" type="success" size="small"
            >领取成功</el-tag
          >
          <el-tag v-if="row.get_result === 0" type="danger" size="small"
            >领取失败</el-tag
          >
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  name: "YunDraw",
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: "/BoxYunDeviceGetRecord/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          minWidth: 45,
          align: "center",
          fixed: "left",
        },
        {
          prop: "member_id",
          label: "用户ID",
          minWidth: 100,
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "user_phone_id",
          label: "云机ID",
          minWidth: 160,
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "get_result",
          label: "领取结果",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 120,
          slot: "get_result",
        },
        {
          prop: "get_time",
          label: "领取时长(单位：小时)",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 160,
        },
        {
          prop: "ip",
          label: "用户IP",
          showOverflowTooltip: true,
          minWidth: 180,
          align: "center",
        },
        {
          prop: "create_time",
          label: "添加时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },

    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        id: row.id,
        status: row.status,
        is_time: true,
      });
      this.$http
        .post("/BoxYunGoods/status", params)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
          } else {
            row.status = !row.status ? 1 : 2;
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
  },
};
</script>

<style scoped></style>
